from tkinter import *
from pathlib import Path

import pymysql

from .server import settings

class Revamp():

    def __init__(self,root,photo23,user=None,passwd=None,logout=None):
        # self.root = Tk()
        # 上为本地调试
        self.logout = logout
        self.user1 = user
        self.passwd1 = passwd
        self.root1 = root
        root.attributes('-disabled', 1)
        self.root = Toplevel(root)
        self.root.title("修改密码")
        self.root.config(background="white")
        self.root.resizable(0, 0)
        root_height = 350
        root_width = 460
        sx = self.root.winfo_screenwidth()
        sy = self.root.winfo_screenheight()
        cw_x = (sx - root_width) / 2
        cw_y = (sy - root_height) / 2
        self.root.geometry("%dx%d+%d+%d" % (root_width, root_height, cw_x, cw_y))

        # photo1 = PhotoImage(file=relative_to_assets("photo23.png"))

        self.canvas_top = Canvas(
            self.root,
            bg="white",
            height=2,
            width=180,
            bd=0,
            highlightthickness=0,
            relief="ridge",
        )
        self.canvas_top.pack(side=TOP, fill=Y)
        Label(self.canvas_top,text = "修改密码", font=("宋", 15, "bold"), fg="#32CDFA",bg = "white").grid(row=0, column=0,
                                                                                           pady = 15)
        Canvas(self.canvas_top,bg ="#32CDFA" ,width = 320,height = 2,relief="ridge" ,bd = 0,highlightthickness=0,).grid(row=1, column=0,
                                                                                           )
        frame0 = Frame(self.root, bg="white", relief="flat", height=20, width=20)
        frame0.pack(side=TOP, fill=Y,pady = 10)
        self.mailbox_l0 = Label(frame0,text = "1.旧密码",bg ="#32CDFA",fg = "white" )
        self.mailbox_l0.grid(row = 0,column = 0)
        Label(frame0,image = photo23,bg = "white").grid(row = 0,column = 1)
        self.pass_l0 = Label(frame0,text = "2.确认密码",bg = "#9bd6f4",fg = "white" )
        self.pass_l0.grid(row = 0,column = 2)

        self.canvas_top_2 = Canvas(
            self.root,
            bg="white",
            height=2,
            width=180,
            bd=0,
            highlightthickness=0,
            relief="ridge", )

        self.canvas_top_2.pack(side=TOP, fill=Y, pady=20)
        self.canvas_top_1 = Canvas(
            self.root,
            bg="white",
            height=2,
            width=180,
            bd=0,
            highlightthickness=0,
            relief="ridge",
        )
        self.canvas_top_1.pack(side=TOP, fill=Y)

        self.cmailbox_l = Label(self.canvas_top_2, bg="white", fg="red", width=30)
        self.cmailbox_l.pack(side=TOP, fill=Y)

        mailbox_l = Label(self.canvas_top_1,bg = "white" , fg = "#32CDFA",text = "账   号:",width = 8)
        mailbox_l.grid(row = 0,column = 0,pady = 10,padx = 15)
        code_l = Label(self.canvas_top_1,bg = "white" , fg = "#32CDFA",text = "旧密码:",width = 8)
        code_l.grid(row = 1,column = 0,pady = 10,padx = 15)

        self.users = StringVar()
        code_e= Entry(self.canvas_top_1, textvariable = self.users,state='disabled',bd=3)
        code_e.grid(row=0, column=1, pady=10, padx=15)
        self.users.set(self.user1)

        self.passwds = StringVar()
        entry1 = Entry(self.canvas_top_1, textvariable=self.passwds,bd = 3)
        entry1.grid(row=1, column=1, pady=10, padx=15)


        next_b = Button(self.canvas_top_1,bg = "#32CDFA",fg = "white",text = "下一步",relief="flat",bd = 0,width = 20,command = self.nekststep)
        next_b.grid(row = 4,column = 0,columnspan=2,pady = 5)

        self.aal = [mailbox_l,code_e,next_b]

        entry1.bind("<Return>", self.nekststep)
        self.root.protocol('WM_DELETE_WINDOW', self.t_close)
        # self.root.mainloop()


    # 下一步函数
    def nekststep(self,enter=""):
        self.cmailbox_l.config(text='')
        passwd = self.passwds.get()
        pwdmd5 = settings.Md5(pwd=passwd)
        ser_password = pwdmd5.strmd5()
        #必须要哈希加密
        if ser_password == str(self.passwd1):
            self.resetpass()
        elif passwd == str(self.passwd1):
            self.resetpass()
        else:
            self.cmailbox_l.config(text='!密码错误')


    def resetpass(self):
        self.mailbox_l0.config(bg = "#9bd6f4")
        self.pass_l0.config(bg = "#32CDFA")

        for i in self.aal:
            i.grid_forget()

        Label(self.canvas_top_1, bg="white", fg="#32CDFA", text="新密码:", width=8).grid(row=0, column=0, pady=10, padx=15)
        Label(self.canvas_top_1, bg="white", fg="#32CDFA", text="确认密码:", width=8).grid(row=1, column=0, pady=10, padx=15)
        self.new_pwd = StringVar()
        a1 = Entry(self.canvas_top_1,textvariable=self.new_pwd, bd=3, )
        a1.grid(row=0, column=1, pady=10, padx=15)

        self.affpwd = StringVar()
        a2 = Entry(self.canvas_top_1, textvariable=self.affpwd,bd=3,)
        a2.grid(row=1, column=1, pady=10, padx=15)
        a2.bind('<Return>', self.ravampwd)
        next_b = Button(self.canvas_top_1,command = self.ravampwd, bg="#32CDFA", fg="white", text="确认修改", relief="flat", bd=0, width=20)
        next_b.grid(row=4, column=0, columnspan=2, pady=5)


    # 确认修改按钮函数
    def ravampwd(self,enter=""):
        self.cmailbox_l.config(text='')
        new_pwd = self.new_pwd.get()
        affpwd = self.affpwd.get()
        r = '^(?=.*?\d)(?=.*[a-z])[0-9a-z]{8,16}$'
        result_paw = re.match(r, new_pwd)
        if not new_pwd:
            self.cmailbox_l.config(text='请输入新密码')
        elif result_paw is None:
            self.cmailbox_l.config(text='!请输入8-16位密码,包含数字和字母')
        elif new_pwd != affpwd:
            self.cmailbox_l.config(text='!两次输入的密码不一样')
        else:
            pwdmd5 = settings.Md5(pwd=new_pwd)
            ser_password = pwdmd5.strmd5()
            try:
                sql = 'UPDATE b_user SET passwd=%s WHERE username=%s'
                value = (ser_password,self.user1)
                sql_data = settings.Mysql_c(sql=sql, value=value)
                sql_data.commit()
                self.root1.attributes('-disabled', 0)
                messagebox.showinfo(title='恭喜', message='密码修改成功，重新登录！')
                self.root.destroy()
                self.logout()

            except pymysql.Error as e:
                print(e)




    def t_close(self):
        self.root1.attributes('-disabled', 0)
        self.root.destroy()

if __name__ == "__main__":
    Revamp()
